package com.itheima.NumberBomb;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameFrame extends JFrame {
    // 游戏逻辑
    private GameLogic gameLogic;
    
    // GUI组件
    private JTextField guessInput;
    private JLabel rangeLabel;
    private JLabel messageLabel;
    private JTextArea historyArea;
    
    public GameFrame() {
        // 初始化游戏逻辑
        gameLogic = new GameLogic();
        
        // 设置窗口属性
        setTitle("数字炸弹游戏");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示
        setLayout(new BorderLayout());
        
        // 初始化组件
        initComponents();
        
        // 添加组件到窗口
        add(createInputPanel(), BorderLayout.NORTH);
        add(createInfoPanel(), BorderLayout.CENTER);
        add(createHistoryPanel(), BorderLayout.SOUTH);
        
        // 显示窗口
        setVisible(true);
    }
    
    private void initComponents() {
        // 输入框
        guessInput = new JTextField(10);
        
        // 标签
        rangeLabel = new JLabel("当前范围: " + gameLogic.getMinRange() + "-" + gameLogic.getMaxRange());
        messageLabel = new JLabel("请输入你的猜测 (1-1000之间的数字)");
        
        // 历史记录区域
        historyArea = new JTextArea(8, 30);
        historyArea.setEditable(false);
        historyArea.setLineWrap(true);
    }
    
    private JPanel createInputPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 猜测按钮
        JButton guessButton = new JButton("猜一猜");
        guessButton.addActionListener(new GuessButtonListener());
        
        // 重新开始按钮
        JButton restartButton = new JButton("重新开始");
        restartButton.addActionListener(e -> restartGame());
        
        panel.add(new JLabel("你的猜测: "));
        panel.add(guessInput);
        panel.add(guessButton);
        panel.add(restartButton);
        
        return panel;
    }
    
    private JPanel createInfoPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        rangeLabel.setFont(new Font("Arial", Font.BOLD, 14));
        messageLabel.setForeground(Color.BLUE);
        
        panel.add(rangeLabel);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(messageLabel);
        
        return panel;
    }
    
    private JScrollPane createHistoryPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder("猜测历史"));
        panel.setLayout(new BorderLayout());
        
        panel.add(new JScrollPane(historyArea), BorderLayout.CENTER);
        
        return new JScrollPane(panel);
    }
    
    private void updateUI() {
        rangeLabel.setText("当前范围: " + gameLogic.getMinRange() + "-" + gameLogic.getMaxRange());
        historyArea.setText(gameLogic.getHistoryText());
    }
    
    private void restartGame() {
        gameLogic.initializeGame();
        updateUI();
        guessInput.setText("");
        messageLabel.setText("请输入你的猜测 (1-100之间的数字)");
        messageLabel.setForeground(Color.BLUE);
    }
    
    private class GuessButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                // 获取用户输入
                int guess = Integer.parseInt(guessInput.getText().trim());
                
                // 处理猜测
                GameLogic.GuessResult result = gameLogic.processGuess(guess);
                
                // 更新消息和颜色
                messageLabel.setText(result.getMessage());
                
                switch (result.getResultType()) {
                    case CORRECT:
                        messageLabel.setForeground(Color.GREEN);
                        break;
                    case TOO_LOW:
                    case TOO_HIGH:
                        messageLabel.setForeground(Color.RED);
                        break;
                    case INVALID_RANGE:
                    case GAME_OVER:
                        messageLabel.setForeground(Color.ORANGE);
                        break;
                }
                
                // 清空输入框并更新UI
                guessInput.setText("");
                updateUI();
                
            } catch (NumberFormatException ex) {
                messageLabel.setText("请输入有效的整数");
                messageLabel.setForeground(Color.ORANGE);
            }
        }
    }
}
    